package ahblue.cn.tanzi.ui.person;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import ahblue.cn.tanzi.R;
import ahblue.cn.tanzi.jmessage.JMessageManage;
import ahblue.cn.tanzi.jmessage.im.util.ToastUtil;
import ahblue.cn.tanzi.model.user.CodeModel;
import ahblue.cn.tanzi.model.user.LoginResultModel;
import ahblue.cn.tanzi.ui.base.BaseActivity;
import ahblue.cn.tanzi.util.CodeCountTimer;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author pink-jackstraw
 * @date 2018/11/16
 * @describe 注册
 */
public class RegistActivity extends BaseActivity {

    @BindView(R.id.etname)
    EditText etname;
    @BindView(R.id.etcode)
    EditText etcode;
    @BindView(R.id.etpwd)
    EditText etpwd;
    @BindView(R.id.einvcode)
    EditText einvCode;
    @BindView(R.id.tvcodeget)
    TextView tvcodeget;
    private CodeCountTimer myCountTimer;

    public static void into(Activity activity) {
        activity.startActivity(new Intent(activity, RegistActivity.class));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist_code);
        ButterKnife.bind(this);
        myCountTimer = new CodeCountTimer(this, tvcodeget);
    }

    private void regist() {
        String name = etname.getText().toString();
        String pwd = etpwd.getText().toString();
        String code = etcode.getText().toString();
        String invCode = einvCode.getText().toString();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(RegistActivity.this, getResources().getString(R.string.no_phone_input), Toast.LENGTH_LONG).show();
        } else if (TextUtils.isEmpty(code)) {
            Toast.makeText(RegistActivity.this, getResources().getString(R.string.no_code_input), Toast.LENGTH_LONG).show();
        }  else if (TextUtils.isEmpty(invCode)) {
            Toast.makeText(RegistActivity.this, getResources().getString(R.string.no_invitecode_input), Toast.LENGTH_LONG).show();
        }  else if (TextUtils.isEmpty(pwd)) {
            Toast.makeText(RegistActivity.this, getResources().getString(R.string.no_pwd_input), Toast.LENGTH_LONG).show();
        } else {
            LoginResultModel.RegistWithCode(this, name, pwd, invCode, code, new LoginResultModel.LoginStatus() {
                @Override
                public void conver(LoginResultModel model) {
                    if (null != model) {
                        JMessageManage.regist(model.member_id,model.invite_code,new JMessageManage.registerResult(){
                            @Override
                            public void success(boolean ok) {
                                if (ok){
                                    ToastUtil.shortToast(RegistActivity.this,"注册成功，请登陆");
                                    LoginActivity.into(RegistActivity.this);
                                    finish();
                                    //注册时更新头像
//                                    final String avatarPath = SharePreferenceManager.getRegisterAvatarPath();
//                                    if (avatarPath != null) {
//                                        ThreadUtil.runInThread(new Runnable() {
//                                            @Override
//                                            public void run() {
//                                                JMessageClient.updateUserAvatar(new File(avatarPath), new BasicCallback() {
//                                                    @Override
//                                                    public void gotResult(int responseCode, String responseMessage) {
//                                                        if (responseCode == 0) {
//                                                            SharePreferenceManager.setCachedAvatarPath(avatarPath);
//                                                            LoginActivity.into(RegistActivity.this);
//                                                            finish();
//                                                        }else {
//                                                            Toast.makeText(RegistActivity.this, "极光注册失败", Toast.LENGTH_SHORT).show();
//                                                            LoginActivity.into(RegistActivity.this);
//                                                            finish();
//                                                        }
//                                                    }
//                                                });
//                                            }
//                                        });
//                                    } else {
//                                        LoginActivity.into(RegistActivity.this);
//                                        SharePreferenceManager.setCachedAvatarPath(null);
//                                    }
                                }else {
                                    Toast.makeText(RegistActivity.this, "极光注册失败", Toast.LENGTH_SHORT).show();
                                    LoginActivity.into(RegistActivity.this);
                                    finish();
                                }
                            }
                        });

                    }else {
                        LoginActivity.into(RegistActivity.this);
                        finish();
                    }
                }
            });
        }
    }

    @OnClick({R.id.regist, R.id.ivfinish, R.id.tvcodeget})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.regist:
                regist();
                break;
            case R.id.ivfinish:
                finish();
                break;
            case R.id.tvcodeget:
                etpwd.requestFocus();
                String name = etname.getText().toString();
                if (TextUtils.isEmpty(name)) {
                    Toast.makeText(RegistActivity.this, getResources().getString(R.string.no_phone_input), Toast.LENGTH_LONG).show();
                } else {
                    CodeModel.getRegistCode(this, etname.getText().toString(), einvCode.getText().toString(), new CodeModel.CodeListener() {
                        @Override
                        public void conver() {
                            myCountTimer.start();
                        }
                    });
                }
                break;
        }
    }
}
